<?php

namespace app\admin\controller;

use app\common\model\package\BabycarPackage as BabycarPackageModel;

/**
 * TP Babycar 套餐管理
 */
class BabycarPackage extends Controller
{
    /**
     * 套餐列表
     * @return \think\response\Json
     */
    public function list()
    {
        $params = $this->request->param();
        $model = new BabycarPackageModel();
        $list = $model->getList($params);
        return json(['code' => 0, 'msg' => 'success', 'data' => $list]);
    }

    /**
     * 套餐详情
     * @param int $package_id 套餐ID
     * @return \think\response\Json
     */
    public function detail($package_id)
    {
        $model = new BabycarPackageModel();
        $detail = $model->getDetail($package_id);
        if (!$detail) {
            return json(['code' => 1, 'msg' => '套餐不存在']);
        }
        return json(['code' => 0, 'msg' => 'success', 'data' => $detail]);
    }

    /**
     * 创建套餐
     * @return \think\response\Json
     */
    public function create()
    {
        $data = $this->request->post();
        // 验证必填字段
        if (empty($data['package_name']) || empty($data['duration_hours']) || 
            empty($data['original_price']) || empty($data['discount_price'])) {
            return json(['code' => 1, 'msg' => '请填写完整套餐信息']);
        }
        // 验证价格
        if ($data['discount_price'] > $data['original_price']) {
            return json(['code' => 1, 'msg' => '折扣价不能大于原价']);
        }
        $model = new BabycarPackageModel();
        $result = $model->createPackage($data);
        if (!$result) {
            return json(['code' => 1, 'msg' => '创建失败']);
        }
        return json(['code' => 0, 'msg' => '创建成功']);
    }

    /**
     * 更新套餐
     * @param int $package_id 套餐ID
     * @return \think\response\Json
     */
    public function update($package_id)
    {
        $data = $this->request->post();
        // 验证价格
        if (isset($data['discount_price']) && isset($data['original_price']) && 
            $data['discount_price'] > $data['original_price']) {
            return json(['code' => 1, 'msg' => '折扣价不能大于原价']);
        }
        $model = new BabycarPackageModel();
        $result = $model->updatePackage($package_id, $data);
        if (!$result) {
            return json(['code' => 1, 'msg' => '更新失败']);
        }
        return json(['code' => 0, 'msg' => '更新成功']);
    }

    /**
     * 删除套餐
     * @param int $package_id 套餐ID
     * @return \think\response\Json
     */
    public function delete($package_id)
    {
        $model = new BabycarPackageModel();
        $result = $model->deletePackage($package_id);
        if (!$result) {
            return json(['code' => 1, 'msg' => '删除失败，套餐可能不存在或已被删除']);
        }
        return json(['code' => 0, 'msg' => '删除成功']);
    }
} 